#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <sys/types.h>

using namespace std;


void clean_func(void*arg)
{
    char*s=(char*)arg;
    std::cout<<"clean up func"<<s<<std::endl;
}

void* signal_handler(void*arg)
{   

    //线程清理
    int execute=(int)arg;
    //只有execute为1的时候才会执行0不会执行
    //压入线程清理函数
#if 0
    pthread_cleanup_push(clean_func,"first push");
    pthread_cleanup_push(clean_func,"second push");
    std::cout<<"thread running id"<<pthread_self()<<std::endl;
    pthread_cleanup_pop(execute);
    pthread_cleanup_pop(execute);
#endif
    return (void*)0;
}

int main(){

    int err;

    pthread_t th1,th2;
    if((err=pthread_create(&th1,NULL,signal_handler,(void*)1))!=0){


        perror("pthread_create");
        exit(1);
    }

    pthread_join(th1,NULL);
    std::cout<<"pthread1 finished"<<std::endl;


    if((err=pthread_create(&th2,NULL,signal_handler,(void*)2))!=0){
        perror("pthread_create");
        exit(1);
    }
    pthread_join(th2,NULL);
    std::cout<<"pthread2 finished"<<std::endl;



}

